package edu.xjdx.os.controller;

import edu.xjdx.os.domain.Cart;
import edu.xjdx.os.domain.CartDetail;
import edu.xjdx.os.domain.Customer;
import edu.xjdx.os.domain.Goods;
import edu.xjdx.os.service.CartService;
import edu.xjdx.os.service.CustomerService;
import edu.xjdx.os.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("cart")
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private GoodsService goodsService;

    @GetMapping("/listAll")
    public String listAll( HttpSession session,Model model) {
        String id=(String) session.getAttribute("customerId");
        Customer customer = customerService.findById(id);
        List<CartDetail> cartList = cartService.findByCustomer(customer);
        Double totalPrice = 0.0;
        for (CartDetail cartDetail : cartList) {
            totalPrice += Double.parseDouble(cartDetail.getTotal());
        }
        Double discountPrice = 0.0;
        for (CartDetail cartDetail : cartList) {
            discountPrice += Double.parseDouble(cartDetail.getDiscount());
        }
        model.addAttribute("discountPrice",discountPrice);
        model.addAttribute("totalPrice", totalPrice);
        model.addAttribute("carts", cartList);
        return "fore/shop-cart";
    }

    @GetMapping("/listAllInOrder")
    public String listAllInOrder(Model model, HttpSession session) {
        String id=session.getAttribute("customerId").toString();
        Customer customer = customerService.findById(id);
        List<CartDetail> cartList = cartService.findByCustomer(customer);
        Double totalPrice = 0.0;
        for (CartDetail cartDetail : cartList) {
            totalPrice += Double.parseDouble(cartDetail.getTotal());
        }
        Double discountPrice = 0.0;
        for (CartDetail cartDetail : cartList) {
            discountPrice += Double.parseDouble(cartDetail.getDiscount());
        }
        model.addAttribute("discountPrice",discountPrice);
        model.addAttribute("totalPrice", totalPrice);
        model.addAttribute("carts", cartList);
        return "fore/check-out";
    }

    @PostMapping("modify")
    @ResponseBody
    public Object modify(String id, Integer amount, HttpSession session) {
        Cart cart = cartService.findById(id);
        Goods goods = goodsService.findById(cart.getGoodsId());
        cartService.modify(id, amount);
        Double total = Double.parseDouble(goods.getGoodsPrice()) * amount;
        String id1=session.getAttribute("customerId").toString();
        Customer customer = customerService.findById(id1);
        List<CartDetail> cartList = cartService.findByCustomer(customer);
        Double totalPrice = 0.0;
        for (CartDetail cartDetail : cartList) {
            totalPrice += Double.parseDouble(cartDetail.getTotal());
        }
        Double discountPrice = 0.0;
        for (CartDetail cartDetail : cartList) {
            discountPrice += Double.parseDouble(cartDetail.getDiscount());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("ok", true);
        map.put("total", total.toString());
        map.put("totalPrice", totalPrice.toString());
        map.put("discountPrice", discountPrice.toString());
        return map;
    }

    @PostMapping("delete")
    @ResponseBody
    public Object delete(String id, HttpSession session) {
        String id1=session.getAttribute("customerId").toString();
        Customer customer = customerService.findById(id1);
        cartService.delete(id);
        List<CartDetail> cartList = cartService.findByCustomer(customer);
        Double totalPrice = 0.0;
        for (CartDetail cartDetail : cartList) {
            totalPrice += Double.parseDouble(cartDetail.getTotal());
        }
        Double discountPrice = 0.0;
        for (CartDetail cartDetail : cartList) {
            discountPrice += Double.parseDouble(cartDetail.getDiscount());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("ok", true);
        map.put("totalPrice", totalPrice.toString());
        map.put("discountPrice", discountPrice.toString());
        return map;
    }
}
